package com.example.project.controller;

import com.example.project.vo.model.PageDto;
import com.example.project.aop.ResponseResult;
import com.example.project.entity.User;
import com.example.project.service.UserService;
import com.example.project.vo.model.ProUserVo;
import com.example.project.vo.model.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.tools.ant.taskdefs.Get;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (User)表控制层
 */
@Api(tags = "User模块")
@ResponseResult
@RequestMapping("user")
public class UserController {

    public static final String modelName = "User";
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;


    @ApiOperation("分页查询查询 " + modelName)
    @PostMapping("/page")
    public PageInfo<User> queryAll(@RequestBody PageDto pageDto) {
        return this.userService.queryAll(pageDto);
    }


    @ApiOperation(modelName + " list查询")
    @GetMapping("/list")
    public List<User> querryList() {
        return this.userService.querryList();
    }

    @ApiOperation("通过主键查询单条 " + modelName + " 数据")
    @GetMapping("/detail")
    public User queryById(@RequestParam("id") Long id) {
        return this.userService.queryById(id);
    }

    @ApiOperation("新增 " + modelName + " 数据")
    @PostMapping("/save")
    public User add(@RequestBody User user) {
        return this.userService.insert(user);
    }

    @ApiOperation("编辑 " + modelName + " 数据")
    @PostMapping("/updateById")
    public User edit(@RequestBody User user) {
        return this.userService.update(user);
    }

    @ApiOperation("通过id 删除 " + modelName + " 数据")
    @DeleteMapping("/delete")
    public Boolean deleteById(@RequestParam("id") Long id) {
        return this.userService.deleteById(id);
    }

    @ApiOperation("获取个人信息")
    @GetMapping("/getMyself")
    public User getMyself() {
        return this.userService.getMyself();
    }


    @ApiOperation("根据部门来获取childrenshu")
    @GetMapping("/tree")
    public List<User> getTree() {
        return this.userService.getTree();
    }


    @ApiOperation("项目关联人操作")
    @PostMapping("/do-pro-user")
    public void doProUser(@RequestBody ProUserVo proUserVo) {
        this.userService.proUser(proUserVo);
    }

    @ApiOperation("根据项目查询用户")
    @GetMapping("/get-pro-user-list")
    public List<User> getProUserList(Long id) {
        return userService.proUserList(id);
    }

    @ApiOperation("根据项目查询用户ids")
    @GetMapping("/get-pro-user-ids")
    public List<Long> getProUserids(Long id) {
        List<Long> collect = userService.proUserList(id).stream().map(User::getId).collect(Collectors.toList());

        return collect;
    }
}

